<template>
  <mdp-lr-box :lspan="currOpType=='select'?24:16">
    <template #left>

    
  <ContentWrap>
    <el-space wrap>
      <mdp-select item-code="postLvl" show-style="origin" v-model="filters.postLvl"
                  placeholder="等级"/>
      <mdp-select item-code="postType" show-style="origin" v-model="filters.postType"
                  placeholder="类型"/>

      <el-input v-model="filters.id" style="width: 15%;" placeholder="编号查询"
                clearable title="支持>、<、 >=、<=、!=、*陈*、$IS NULL、$IN 1,2,3、between 1,5等操作符"/>
      <el-input v-model="filters.postName" style="width: 15%;"
                placeholder="名称查询" clearable
                title="支持>、<、 >=、<=、!=、*陈*、$IS NULL、$IN 1,2,3、between 1,5等操作符"/>

      <el-button v-loading="load.list" :disabled="load.list==true" @click="searchTableDatas()"
                 icon="search" type="primary">查询
      </el-button>
    </el-space> 
    <template #header>
    <el-space wrap>
      <mdp-hi-query :column-configs="columnConfigs" show-style="tag" v-model="hiQueryParams"
                    @change="onHiQueryParamsChange"/>
      <el-button icon="zoom-out" @click="searchReset()" title="重置查询条件"/>
     
      <el-button icon="download" @click="export2Excel()" title="导出" />
      <mdp-table-configs :column-configs="columnConfigs" show-style="tag"
                        v-model="checkedColumns"/>
        <span  v-if="currOpType=='mng'">
          <el-button
            :disabled="disBtn('delBtn') || !checkBtnQx('delBtn',menuDefId) || this.sels.length===0 || load.del==true"
            type="danger" plain v-loading="load.del" @click="batchDel" icon="delete"/>
          <el-button :disabled="disBtn('addBtn') || !checkBtnQx('addBtn',menuDefId) "
                    type="primary" plain
                    @click="openForm({parentOpType:currOpType,subOpType:'add',formData:addForm})"
                    icon="plus"/>
      </span>
      <span  v-else-if="currOpType=='select' &&  this.multiple==true">
                  <el-button :disabled=" this.sels.length===0"
                            type="primary" @click="selectListConfirm" icon="check"/>
                            
              
      </span>
      <el-button @click="$refs['kanbanDlg'].open()"  title="看板" type="success" plain>板</el-button>  
    </el-space> 
    </template>
  </ContentWrap> 
    <!--列表 Post 岗位管理-->
    <el-table v-adaptive :ref="refId+'Table'" :data="tableDatas" @sort-change="sortChange"
              highlight-current-row v-loading="load.list" border @selection-change="selsChange"
              @row-click="rowClick" style="width: 100%;">
      <el-table-column type="selection" width="55" show-overflow-tooltip fixed="left"
                       v-if="currOpType=='mng' || this.multiple==true"/>
      <el-table-column sortable type="index" width="55" show-overflow-tooltip fixed="left"/>
      <el-table-column prop="postName" label="名称" min-width="180"  
                       col-type="String" v-if="showCol('postName')">
        <template #default="scope"> 
                     <el-link type="primary" @click="openForm({parentOpType:currOpType,subOpType:'edit',formData:scope.row})">  {{ scope.row.postName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="120" show-overflow-tooltip/>
      <el-table-column prop="postLvl" label="岗位级别" width="120" show-overflow-tooltip item-code="postLvl"
                       col-type="Integer" v-if="showCol('postLvl')">
        <template #default="scope">
          <mdp-select @click="rowClick(scope.row)" item-code="postLvl" show-style="tag"
                      v-model="scope.row.postLvl" :precision="0" @change="editSomeFields(scope.row,'postLvl',$event)"
                      :disabled="disField"/>
        </template>
      </el-table-column>
      <el-table-column prop="postType" label="岗位类型" width="120" show-overflow-tooltip item-code="postType"
                       col-type="String" v-if="showCol('postType')">
        <template #default="scope">
          <mdp-select @click="rowClick(scope.row)" item-code="postType" show-style="tag"
                      v-model="scope.row.postType" :maxlength="20"
                      @change="editSomeFields(scope.row,'postType',$event)"
                      :disabled="disField"/>
        </template>
      </el-table-column>
      <el-table-column prop="cdate" label="创建日期" width="120" show-overflow-tooltip
                       col-type="Date" v-if="showCol('cdate')">
        <template #default="scope">
          <mdp-date type="date" placeholder="选择日期" show-style="tag" v-model="scope.row.cdate"
                    value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD"
                    :disabled="disField ||true"/>
        </template>
      </el-table-column>
      <el-table-column prop="branchId" label="归属机构号" min-width="120" show-overflow-tooltip
                       col-type="String" v-if="showCol('branchId')">
        <template #default="scope">
          <mdp-input show-style="tag" v-model="scope.row.branchId" :maxlength="50"
                     @change="editSomeFields(scope.row,'branchId',$event)"
                     :disabled="disField||true"/>
        </template>
      </el-table-column>
      
      <el-table-column prop="id" label="编号" min-width="120" show-overflow-tooltip 
                       col-type="String" v-if="showCol('id')"/>
      <el-table-column label="操作" :width="currOpType=='mng'?100:100" fixed="right">
        <template #default="scope" v-if="currOpType=='mng'">
 
          <el-button size="mini"
                     @click="$refs['postUserListDialog'].open({ filters:{postId:scope.row.id},subOpType:'postUserMng'})"
                     icon="user" title="查看角色拥有的用户"/> 
        </template>

        <template #default="scope" v-else-if="currOpType=='list'">
          <el-button type="primary"
                     @click="openForm({parentOpType:currOpType,subOpType:'detail',formData:scope.row})"
                     icon="view"/>
        </template>

        <template #default="scope" v-else-if="currOpType=='select' && this.multiple!=true">
          <el-button type="primary" @click="selectConfirm(scope.row)" icon="check"/>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="padding-top"
      layout="slot,total, sizes, prev, next,pager,jumper"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :page-sizes="[10,20, 50, 100, 500]"
      :current-page="pageInfo.pageNum"
      :page-size="pageInfo.pageSize"
      :total="pageInfo.total" 
    /> 
  </template>
  <PostRoleIndex v-if="currOpType!='select' && editForm.id" :params="{postId:editForm.id}"/>
  </mdp-lr-box>
  <!--新增修改明细 Post 岗位管理界面-->
  <mdp-dialog :ref="refId+'FormDialog'">
    <template #default="{visible,data}">
      <post-form :ref="refId+'Form'" :visible="visible" :parent-op-type="currOpType"
                 :sub-op-type="data.subOpType" :form-data="data.formData" @close="onFormClose"
                 @submit="afterFormSubmit" @fields-change="afterEditSomeFields"
                 :show-btn="false"/>
    </template>
    <template #footer="{data}">
      <el-button @click="$refs[refId+'FormDialog'].close()">关闭</el-button>
      <el-button v-if="data.subOpType=='add'"
                 :disabled=" disBtn('addBtn') || !checkBtnQx('addBtn',menuDefId) "
                 type="primary" @click="$refs[refId+'Form'].saveSubmit()">提交
      </el-button>
    </template>
  </mdp-dialog>
  <SetRolesToPost ref="SetRolesToPost"/>
  <mdp-dialog ref="postUserListDialog" width="85%">
    <template #default="{visible,data}">
      <DeptPostUserIndex :visible="visible" :params="data.filters" :subOpType="data.subOpType"/>
    </template>
  </mdp-dialog>
  <mdp-dialog ref="kanbanDlg" :title="'岗位看板'" fullscreen append-to-body :close-on-click-modal="false">
    <MdpTableKanban  @del="(d,cb)=>handleDel(d,0,cb)"
      @plus="(cb)=>openForm({parentOpType:currOpType,subOpType:'add',formData:addForm})"
      @edit="(d,cb)=>openForm({subOpType:'edit',formData:d,callback:cb})"
      v-model="tableDatas" 
      :columnConfigs="columnConfigs"
      itemKey="id" 
      :contentFun="(item)=>item.postName"
    > 
    <template #toolbar="{element}">
      <MdpSelect showStyle="tag" itemCode="postLvl" disabled v-model="element.postLvl"/>
    </template>
    </MdpTableKanban>
  </mdp-dialog>
</template>

<script>

import util from '@/components/mdp-ui/js/util';//全局公共库
import {MdpTableMixin} from '@/components/mdp-ui/mixin/MdpTableMixin.js';
import config from '@/api/mdp_pub/mdp_config';//全局公共库
import * as PostApi from '@/api/mdp/sys/post';
import PostForm from './Form.vue';//新增修改明细界面
import SetRolesToPost from "../postRole/SetRolesToPost.vue";//设置岗位的角色
import DeptPostUserIndex from "../deptPostUser/Index.vue";//设置岗位的角色
import {mapState} from 'pinia'
import {useUserStore} from '@/store/modules/user'
import PostRoleIndex from '../postRole/Index.vue'
export default {
  name: 'postMng',
  mixins: [MdpTableMixin],
  components: {
    PostForm, SetRolesToPost, DeptPostUserIndex,PostRoleIndex
  },
  computed: {
    ...mapState(useUserStore, ['userInfo'])
  },
  watch: {},
  data() {
    return {
      menuDefId: '',//menu_def.id 菜单表菜单编号，用于按钮权限判断
      menuDefName: '岗位管理',//menu_def.name 功能名称，用于导出excel等文件名
      refId: 'post',//引用编号，<table :ref="refId+'Table'"> <form :ref="refId+'Form'">
      pkNames: ["id"],//表格主键的java属性名称，驼峰命名，默认为id,支持多主键
      currOpType: 'mng',//表格 mng-综合管理具有最大权限，所有按钮可动、detail-只看不能操作
      filters: {//查询参数

      },
      defaultFilters: {//默认查询参数,第一次打开界面的时候用到，恢复默认值的时候用到

      },

      defaultCheckColumnNum: 8,//默认展示的表格列数，前8列

      //增删改查(含批量)接口
      apis: {
        list: PostApi.listPost,
        add: PostApi.addPost,
        del: PostApi.delPost,
        edit: PostApi.editPost,
        editSomeFields: PostApi.editSomeFieldsPost,
        batchAdd: PostApi.batchAddPost,
        batchDel: PostApi.batchDelPost,
        batchEdit: PostApi.batchEditPost,
      }
    }
  },
  methods: {
    //页面初始化需要配置的特殊逻辑写这里
    initCurrData() {
      this.searchTableDatas();
    },

    /**
     * 检查参数是否满足调用后台接口的条件
     *
     * @param params 提交给后台的参数池,map类型
     * @returns true / false
     */
    preQueryParamCheck(params) {
      params.branchId = this.userInfo.branchId
      return true;
    },

    //页面数据加载完后需要对数据进行加工处理的
    afterList(res, isOk, apiName) {

    },

    /**
     * 对修改的字段进行判断，返回false ,将取消更新数据库
     * @param {*} row 当前选中的行
     * @param {*} fieldName 修改的字段名
     * @param {*} $event 修改后的值
     * @param {*} params 将要提交服务器的参数
     * @returns true/false 返回false ,将取消更新数据库
     */
    editSomeFieldsCheck(row, fieldName, $event, params) {
      if (this.currOpType == 'add' || this.currOpType == 'detail') {
        return false;
      }
      params[fieldName] = $event
      return true;
    },

    /**
     * 打开某个子页面
     */
    openForm(res) {
      let {parentOpType, formRefId, subOpType, formData} = res
      if (!formRefId) {
        formRefId = this.getRefId() + "FormDialog"
      }
      if (!parentOpType) {
        parentOpType = this.currOpType
      }
      var form = this.$refs[formRefId]
      if (form) {
        res.parentOpType = parentOpType
        res.formRefId = formRefId
        form.open(res);
      } else {

      }

    },
    showDeptPostUserMng(row) {

    }
  },
  mounted() {

  }
}

</script>

<style scoped>
</style>
